/*
 * @Description: egg核心处理方法 tf egg
 * @Author: 听风
 * @Date: 2020-12-22 13:29:32
 */

const { restfulRouter, createEggOptionFile, eggCreateAll } = require('../script/egg');

const action = async (filename, options) => {
  if (filename) {
    // 全部增加
    await eggCreateAll(filename);
    return;
  }
  const [type, fname] = Object.entries(options)[0];
  if (type === 'router') {
    await restfulRouter(fname);
  } else if (type === 'all') {
    // 全部增加
    await eggCreateAll(fname);
  } else {
    await createEggOptionFile(type, fname);
  }
};

const egg = program => {
  const eggOptions = ['controller', 'service', 'model', 'router', 'all'];

  let optionsStr = '';

  eggOptions.forEach(e => {
    optionsStr += `.option('-${e[0]}, --${e} <filename>', '添加egg的${e}文件')`;
  });

  const evalStr = `
    program
      .command('egg [filename]')
      ${optionsStr}
      .action((filename, options) => {
        action(filename, options);
      });
    `;

  eval(evalStr);
};

module.exports = egg;
